package com.ruoyi.project.module.whd.jiezhang;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/whd/jiezhang")
public class WhdJiezhangController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgMembershipFormService yqgMembershipFormService;
    @Autowired
    private IYqgRechargeConsumptionService yqgRechargeConsumptionService;
    @Autowired
    private IYqgCheckoutMethodService yqgCheckoutMethodService;



    /**
     * 查询消费详细列表
     */
   /* @RequiresPermissions("project:whd:jiezhang:list")*/
    @GetMapping("/list")
    public TableDataInfo list(YqgBarBills yqgBarBills)
    {
        yqgBarBills.setHandState("1");
        YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        if(list.size()==0){
            return  getDataTable(new ArrayList<YqgConsumptionDetails>());
        }
        YqgHands hand=new YqgHands();
        hand.setHandBrand(yqgBarBills.getHandBrand());
        hand.setLockedState("0");
        List<YqgHands> hlist=yqgHandsService.selectYqgHandsList(hand);
        //并牌没解锁
        if(list.get(0).getMergeStatus().equals("0")&&hlist.size()!=0){
           return  getDataTable(new ArrayList<YqgConsumptionDetails>());
        }
        //并牌已经解锁的
        else if(list.get(0).getMergeStatus().equals("0")&&hlist.size()==0){
            yqgConsumptionDetails.setBarBillsId(list.get(0).getID());
            yqgConsumptionDetails.setHandState("1");
            yqgConsumptionDetails.setConsumptionStatus("0");
            List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            //过滤并牌
            List<YqgConsumptionDetails> bplist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){

                if(ylist.get(i).getMergeBarBillsId()!=null){
                    bplist.add(ylist.get(i));
                }
            }
            ylist.removeAll(bplist);
            //显示转化
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getBeforeBarBillsId()!=null){
                    ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
                }
            }
            //过滤过夜费
            List<YqgConsumptionDetails> gylist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getEntryName().equals("过夜费")){
                    Date retime= ylist.get(i).getRecordingTime();
                    LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                    String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                    String start=date+" 00:00:00";
                    String end=date+" 06:00:00";
                    Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                    Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                    LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                    LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                    if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                        LocalDateTime newDateTime = Record.plusHours(2);
                        LocalDateTime currentDateTime = LocalDateTime.now();
                        if(currentDateTime.isBefore(newDateTime)){
                            gylist.add(ylist.get(i));
                        }

                    }

                }
            }
            ylist.removeAll(gylist);
            return getDataTable(ylist);
        }
        //没有并牌的
        else{
            yqgConsumptionDetails.setBarBillsId(list.get(0).getID());
            yqgConsumptionDetails.setHandState("1");
            yqgConsumptionDetails.setConsumptionStatus("0");

            List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getBeforeBarBillsId()!=null){
                    ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
                }
            }
            //并牌查询
            yqgConsumptionDetails.setBarBillsId(null);
            yqgConsumptionDetails.setMergeBarBillsId(list.get(0).getID());
            List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            List<YqgConsumptionDetails> removelist=new ArrayList<>();
            for(int i=0;i< blist.size();i++){

                if(blist.get(i).getMergeBarBillsId().equals(list.get(0).getID())&&blist.get(i).getBarBillsId().equals(list.get(0).getID())){
                    removelist.add(blist.get(i));
                }
            }
            if(removelist.size()!=0){
                blist.removeAll(removelist);
            }
            for(int i=0;i<blist.size();i++){
                if(blist.get(i).getMergeBarBillsId()!=null){
                    blist.get(i).setNote10("来自"+blist.get(i).getHandBrand()+"的并牌");
                }
            }
            if(blist.size()==0){
                //过滤过夜费
                List<YqgConsumptionDetails> gylist=new ArrayList<>();
                for(int i=0;i<ylist.size();i++){
                    if(ylist.get(i).getEntryName().equals("过夜费")){
                        Date retime= ylist.get(i).getRecordingTime();
                        LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                        String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                        String start=date+" 00:00:00";
                        String end=date+" 06:00:00";

                        Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                        Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                        LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                        LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                        if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                            LocalDateTime newDateTime = Record.plusHours(2);
                            LocalDateTime currentDateTime = LocalDateTime.now();
                            if(currentDateTime.isBefore(newDateTime)){
                                gylist.add(ylist.get(i));
                            }

                        }

                    }
                }
                ylist.removeAll(gylist);
                return getDataTable(ylist);
            }
            else{
                //增加并牌数据
                ylist.addAll(blist);
                //过滤过夜费
                List<YqgConsumptionDetails> gylist=new ArrayList<>();
                for(int i=0;i<ylist.size();i++){
                    if(ylist.get(i).getEntryName().equals("过夜费")){
                        Date retime= ylist.get(i).getRecordingTime();
                        LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                        String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                        String start=date+" 00:00:00";
                        String end=date+" 06:00:00";
                        Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                        Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                        LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                        LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                        if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                            LocalDateTime newDateTime = Record.plusHours(2);
                            LocalDateTime currentDateTime = LocalDateTime.now();
                            if(currentDateTime.isBefore(newDateTime)){
                                gylist.add(ylist.get(i));
                            }

                        }

                    }
                }
                ylist.removeAll(gylist);
                return getDataTable(ylist);
            }

        }

    }

    /**
     * 导出消费详细列表
     */
  /*  @RequiresPermissions("project:whd:jiezhang:export")*/
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
   /* @RequiresPermissions("project:whd:jiezhang:query")*/
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
   /* @RequiresPermissions("project:whd:jiezhang:add")*/
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

   /* @RequiresPermissions("project:whd:jiezhang:remove")*/
    @Log(title = "结账", businessType = BusinessType.DELETE)
    @PutMapping
    public AjaxResult pay(@RequestBody YqgBarBills yqgBarBills)
    {
        int flag=1;

        flag=checklist(yqgBarBills);
        if(flag==1){
            return new AjaxResult(500,"账单中存在未下钟的消费记录，请下钟后结账");
        }else if(flag==2) {
            return new AjaxResult(500,"账单中存在未退房的消费记录，请退房后结账");
        }else if(flag==3) {
        return new AjaxResult(500,"账单中存在未退浴袍的消费记录，请退浴袍后结账");
       }else{
                //支付的
                Double paid=Double.valueOf(yqgBarBills.getPaidInAmount());
                //总费用
                Double total=Double.valueOf(yqgBarBills.getTotalConsumptionAmount());
                Double methodAmount=Double.valueOf(yqgBarBills.getAllCost());
                if(total-paid-methodAmount==0){
                    //如果结清，清表

                    insertmethod(yqgBarBills);
                    return edit(yqgBarBills);
                }
                else{
                    //如果没有，插入一条分开结账方式记录

                    insertmethod(yqgBarBills);
                    return new AjaxResult(200,"结账成功");
                }
            }


    }
    public int checklist(YqgBarBills yqgBarBills){
        String HandBrand=yqgBarBills.getHandBrand();
        YqgBarBills BarBills=new YqgBarBills();
        BarBills.setHandBrand(HandBrand);
        BarBills.setHandState("1");
        YqgConsumptionDetails ConsumptionDetails=new YqgConsumptionDetails();
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(BarBills);
        List<YqgConsumptionDetails> ylist=new ArrayList<>();
        LoginUser loginUser = getLoginUser();
        YqgHands hand=new YqgHands();
        hand.setHandBrand(yqgBarBills.getHandBrand());
        hand.setLockedState("0");
        List<YqgHands> handlist=yqgHandsService.selectYqgHandsList(hand);
        if(!list.get(0).getMergeStatus().equals("0")){

            ConsumptionDetails.setBarBillsId(list.get(0).getID());
            ConsumptionDetails.setHandState("1");
            ConsumptionDetails.setConsumptionStatus("0");

            ylist=yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
            //并牌查询
            ConsumptionDetails.setBarBillsId(null);
            ConsumptionDetails.setMergeBarBillsId(list.get(0).getID());
            List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);

            List<YqgConsumptionDetails> removelist=new ArrayList<>();
            for(int i=0;i< blist.size();i++){

                if(blist.get(i).getMergeBarBillsId().equals(list.get(0).getID())&&blist.get(i).getBarBillsId().equals(list.get(0).getID())){
                    removelist.add(blist.get(i));
                }
            }
            if(removelist.size()!=0){
                blist.removeAll(removelist);
            }

            if(blist.size()==0){

            }
            else{
                ylist.addAll(blist);

            }

        }
        else if(list.get(0).getMergeStatus().equals("0")&&handlist.size()==0){

            ConsumptionDetails.setBarBillsId(list.get(0).getID());
            ConsumptionDetails.setHandState("1");
            ConsumptionDetails.setConsumptionStatus("0");
            ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
            List<YqgConsumptionDetails> blist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){

                if(ylist.get(i).getMergeBarBillsId()!=null){
                    blist.add(ylist.get(i));
                }
            }
            ylist.removeAll(blist);
        }
        /*if(ylist.size()==0){
            return new AjaxResult(500,"空表");
        }*/
        //判断 下钟 退房
        for(int i=0;i<ylist.size();i++){
            if(ylist.get(i).getNextClockStatus()!=null){
                if(ylist.get(i).getNextClockStatus().equals("1")){
                    return 1;
                }
            }
            if(ylist.get(i).getCheckOutStatus()!=null){
                if(ylist.get(i).getCheckOutStatus().equals("1")){
                    return 2;
                }
            }
            if(ylist.get(i).getEntryName().contains("浴服押金")){
                return 3;
            }


        }
        return  0;
    }

    public  void insertmethod(YqgBarBills yqgBarBills){
        YqgCheckoutMethod yqgCheckoutMethod=new YqgCheckoutMethod();
        //查询吧台账单表
        YqgBarBills bills=new YqgBarBills();
        bills.setHandBrand(yqgBarBills.getHandBrand());
        bills.setHandState("1");
        List<YqgBarBills> barlist = yqgBarBillsService.selectYqgBarBillsList(bills);
        yqgCheckoutMethod.setBarBillsId(barlist.get(0).getID());
        yqgCheckoutMethod.setHandBrand(yqgBarBills.getHandBrand());

        //用户信息
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        yqgCheckoutMethod.setUserId(sysUser.getUserId());
        yqgCheckoutMethod.setDeptId(sysUser.getDeptId());
        yqgCheckoutMethod.setNickName(sysUser.getNickName());
        yqgCheckoutMethod.setPost(sysUser.getRemark());
        yqgCheckoutMethod.setUserName(sysUser.getUserName());
        yqgCheckoutMethod.setDepartment("3");
        //
        yqgCheckoutMethod.setClosingTime(DateUtils.getNowDate());
        yqgCheckoutMethod.setPaymentMethod(yqgBarBills.getPaymentMethod());
        yqgCheckoutMethod.setPaymentAmount(yqgBarBills.getPaidInAmount());
        yqgCheckoutMethodService.insertYqgCheckoutMethod(yqgCheckoutMethod);
    }
    /**
     * 修改消费详细
     */
   /* @RequiresPermissions("project:whd:jiezhang:edit")*/
    @Log(title="结账",businessType = BusinessType.UPDATE,logicalKey = "handBrand")
    public AjaxResult edit(@RequestBody YqgBarBills yqgBarBills)
    {
        //结账方式
        YqgMembershipForm yqgMembershipForm=new YqgMembershipForm();
        YqgRechargeConsumption yqgRechargeConsumption=new YqgRechargeConsumption();

        String paymethod=yqgBarBills.getPaymentMethod();
        String HandBrand=yqgBarBills.getHandBrand();
        YqgBarBills BarBills=new YqgBarBills();
        BarBills.setHandBrand(HandBrand);
        BarBills.setHandState("1");
        YqgConsumptionDetails ConsumptionDetails=new YqgConsumptionDetails();
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(BarBills);
        List<YqgConsumptionDetails> ylist=new ArrayList<>();
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        YqgHands hand=new YqgHands();
        if(yqgBarBills.getPaymentMethod().equals("7")){
            System.out.println(yqgBarBills.getCardNumber());
            yqgMembershipForm.setCardNumber(yqgBarBills.getCardNumber());
            System.out.println(yqgMembershipForm.getCardNumber());
            //这里需要一个卡类型
            List<YqgMembershipForm> mlist = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);
            if(mlist.size()!=0){
                yqgMembershipForm= mlist.get(0);
                if(Integer.valueOf(yqgMembershipForm.getResidualAmount())>Integer.valueOf(yqgBarBills.getTotalConsumptionAmount())){
                    int resamount=Integer.valueOf(yqgMembershipForm.getResidualAmount())-Integer.valueOf(yqgBarBills.getTotalConsumptionAmount());
                    yqgMembershipForm.setResidualAmount(String.valueOf(resamount));
                    int accconsumption=Integer.valueOf(yqgMembershipForm.getAccumulatedConsumption());
                    accconsumption=accconsumption+Integer.valueOf(yqgBarBills.getTotalConsumptionAmount());
                    yqgMembershipForm.setAccumulatedConsumption(String.valueOf(accconsumption));
                    int resintegral=Integer.valueOf(yqgMembershipForm.getResidualIntegral());
                    resintegral=resintegral+Integer.valueOf(yqgBarBills.getTotalConsumptionAmount());
                    yqgMembershipForm.setResidualIntegral(String.valueOf( resintegral));
                    yqgMembershipFormService.updateYqgMembershipForm(yqgMembershipForm);


                }else{
                    return new AjaxResult(500,"卡内余额不足");
                }
                //获取
                yqgRechargeConsumption.setCardNumber(yqgMembershipForm.getCardNumber());
                yqgRechargeConsumption.setName(yqgMembershipForm.getName());
                yqgRechargeConsumption.setMobilePhone(yqgMembershipForm.getMobilePhone());
                yqgRechargeConsumption.setType(yqgMembershipForm.getType());
                yqgRechargeConsumption.setCardNature(yqgMembershipForm.getCardNature());
                //自填
                yqgRechargeConsumption.setAmountConsumption(yqgBarBills.getTotalConsumptionAmount());
                yqgRechargeConsumption.setTypeOperation("1");
                yqgRechargeConsumption.setOperator(sysUser.getUserName());
                yqgRechargeConsumption.setOperatingTime(DateUtils.getNowDate());
                yqgRechargeConsumptionService.insertYqgRechargeConsumption(yqgRechargeConsumption);
            }
            else{
                return new AjaxResult(500,"卡号输入错误");
            }



            System.out.println(yqgBarBills.getTotalConsumptionAmount());
            System.out.println(yqgBarBills.getNote10());
        }
        else if(yqgBarBills.getPaymentMethod().equals("16")){
            yqgMembershipForm.setCardNumber(yqgBarBills.getCardNumber());
            //这里需要一个卡类型
            List<YqgMembershipForm> mlist = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);
            if(mlist.size()!=0){
                yqgMembershipForm= mlist.get(0);
                if(Integer.valueOf(yqgMembershipForm.getResidualAmount())>0){
                    int resamount=Integer.valueOf(yqgMembershipForm.getResidualAmount())-1;
                    yqgMembershipForm.setResidualAmount(String.valueOf(resamount));
                    int accconsumption=Integer.valueOf(yqgMembershipForm.getAccumulatedConsumption());
                    accconsumption=accconsumption+1;
                    yqgMembershipForm.setAccumulatedConsumption(String.valueOf(accconsumption));
                    yqgMembershipFormService.updateYqgMembershipForm(yqgMembershipForm);


                }else{
                    return new AjaxResult(500,"卡内次数不足");
                }
                //获取
                yqgRechargeConsumption.setCardNumber(yqgMembershipForm.getCardNumber());
                yqgRechargeConsumption.setName(yqgMembershipForm.getName());
                yqgRechargeConsumption.setMobilePhone(yqgMembershipForm.getMobilePhone());
                yqgRechargeConsumption.setType(yqgMembershipForm.getType());
                yqgRechargeConsumption.setCardNature(yqgMembershipForm.getCardNature());
                //自填
                yqgRechargeConsumption.setAmountConsumption(yqgBarBills.getTotalConsumptionAmount());
                yqgRechargeConsumption.setTypeOperation("1");
                yqgRechargeConsumption.setOperator(sysUser.getUserName());
                yqgRechargeConsumption.setOperatingTime(DateUtils.getNowDate());
                yqgRechargeConsumptionService.insertYqgRechargeConsumption(yqgRechargeConsumption);
            }
            else{
                return new AjaxResult(500,"卡号输入错误");
            }
            System.out.println(yqgBarBills.getNote10());
        }
        hand.setHandBrand(yqgBarBills.getHandBrand());
        hand.setLockedState("0");
        List<YqgHands> handlist=yqgHandsService.selectYqgHandsList(hand);

        if(!list.get(0).getMergeStatus().equals("0")){

            ConsumptionDetails.setBarBillsId(list.get(0).getID());
            ConsumptionDetails.setHandState("1");
            ConsumptionDetails.setConsumptionStatus("0");

            ylist=yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
            //并牌查询
            ConsumptionDetails.setBarBillsId(null);
            ConsumptionDetails.setMergeBarBillsId(list.get(0).getID());
            List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);

            List<YqgConsumptionDetails> removelist=new ArrayList<>();
            for(int i=0;i< blist.size();i++){

                if(blist.get(i).getMergeBarBillsId().equals(list.get(0).getID())&&blist.get(i).getBarBillsId().equals(list.get(0).getID())){
                    removelist.add(blist.get(i));
                }
            }
            if(removelist.size()!=0){
                blist.removeAll(removelist);
            }

            if(blist.size()==0){

            }
            else{
                ylist.addAll(blist);

            }

        }
        else if(list.get(0).getMergeStatus().equals("0")&&handlist.size()==0){

            ConsumptionDetails.setBarBillsId(list.get(0).getID());
            ConsumptionDetails.setHandState("1");
            ConsumptionDetails.setConsumptionStatus("0");
            ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
            List<YqgConsumptionDetails> blist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){

                if(ylist.get(i).getMergeBarBillsId()!=null){
                    blist.add(ylist.get(i));
                }
            }
            ylist.removeAll(blist);
        }
        /*System.out.println("是这吗？");
        System.out.println(ylist.size());*/
        /*if(ylist.size()==0){
            return new AjaxResult(500,"空表");
        }*/
        //判断 下钟 退房
        /*for(int i=0;i<ylist.size();i++){
            if(ylist.get(i).getNextClockStatus()!=null){
                if(ylist.get(i).getNextClockStatus().equals("1")){
                    return new AjaxResult(500,"账单中存在未下钟的消费记录，请下钟后结账");
                }
            }
            if(ylist.get(i).getCheckOutStatus()!=null){
                if(ylist.get(i).getCheckOutStatus().equals("1")){
                    return new AjaxResult(500,"账单中存在未退房的消费记录，请退房后结账");
                }
            }


        }*/
        //记录未结账的过夜费费用
        long total=0;
        //未过两小时的过夜费去掉

        for(int i=0;i<ylist.size();i++){

            if(ylist.get(i).getEntryName().equals("过夜费")){
                Date retime= ylist.get(i).getRecordingTime();
                LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                String start=date+" 00:00:00";
                String end=date+" 06:00:00";
                Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                    LocalDateTime newDateTime = Record.plusHours(2);
                    LocalDateTime currentDateTime = LocalDateTime.now();
                    if(currentDateTime.isBefore(newDateTime)){
                        ylist.get(i).setConsumptionStatus("1");
                        yqgConsumptionDetailsService.updateYqgConsumptionDetails(ylist.get(i));
                        total=total+Long.valueOf(ylist.get(i).getTotalAmount());
                    }

                }
                else{
                    total=total+Long.valueOf(ylist.get(i).getTotalAmount());
                }

            }
            else{
                total=total+Long.valueOf(ylist.get(i).getTotalAmount());
            }


        }
        //查询吧台账单表
        YqgBarBills bills=new YqgBarBills();
        bills.setHandBrand(HandBrand);
        bills.setHandState("1");
        List<YqgBarBills> barlist = yqgBarBillsService.selectYqgBarBillsList(bills);
        for(int i=0;i<ylist.size();i++){
            //更新消费信息
            ylist.get(i).setHandState("0");//空牌
            /*list.get(i).setConsumptionStatus("1");*/
            ylist.get(i).setPaymentMethod(paymethod);//结账方式
            //交班
            ylist.get(i).setHandoverStatus("1");
            ylist.get(i).setSailingsSigns("1");
           /*  if(barlist.get(0).getHandoverStatus().equals("1")){

            }*/
            yqgConsumptionDetailsService.updateYqgConsumptionDetails(ylist.get(i));

        }
        //最后处理结账人的信息
        YqgHands Hands=new YqgHands();
        Hands.setHandBrand(HandBrand);
        Hands.setHandState("1");
        List<YqgHands> hlist = yqgHandsService.selectYqgHandsList(Hands);

        if(hlist.size()!=0){
            Hands=hlist.get(0);
            //空牌
            Hands.setHandState("0");
            //解锁
            Hands.setLockedState("1");
            yqgHandsService.updateYqgHands(Hands);
        }

        //更新吧台账单表
        if(barlist.size()!=0){
            barlist.get(0).setAllCost(String.valueOf(total));
            barlist.get(0).setCheckoutTime(DateUtils.getNowDate());
            barlist.get(0).setPaymentMethod(yqgBarBills.getPaymentMethod());
            barlist.get(0).setTotalConsumptionAmount(String.valueOf(total));
            barlist.get(0).setDiscountAmount(yqgBarBills.getDiscountAmount());
            barlist.get(0).setSupplementaryConsumption(yqgBarBills.getSupplementaryConsumption());
            barlist.get(0).setPaidInAmount(String.valueOf(total));
            barlist.get(0).setPaidInCash(yqgBarBills.getPaidInCash());
            barlist.get(0).setChangeAmount(yqgBarBills.getChangeAmount());
            barlist.get(0).setCheckout(sysUser.getUserName());
//            barlist.get(0).setHandoverStatus("0");
            //空牌
            barlist.get(0).setHandState("0");
            yqgBarBillsService.updateYqgBarBills(barlist.get(0));
        }
        return new AjaxResult(200,"结账成功");
    }
    /**
     * 删除消费详细
     */
    /*@RequiresPermissions("project:whd:jiezhang:remove")*/
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
    /**
     * 未下钟 未退房不能结账  查库
     */
    public int check(String handbrand,String mergeStatus,String retentionStatus){
        // handbrand 手牌号 mergeStatus 并牌状态 RetentionStatus 留牌状态
        //查询 根据手牌号 和 并牌 留牌状态 唯一确定一个
        //2未退房  3未下钟 1全退
        YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();

        if(retentionStatus=="0"){
            //留牌查留前手牌
            yqgConsumptionDetails.setBeforeLeave(handbrand);
        }else{
            //并牌查手牌号
            yqgConsumptionDetails.setHandBrand(handbrand);
        }

        yqgConsumptionDetails.setHandState("1");
        yqgConsumptionDetails.setMergeStatus(mergeStatus);
        yqgConsumptionDetails.setRetentionStatus(retentionStatus);
        //设置未下钟
        yqgConsumptionDetails.setNextClockStatus("1");
        //自己的账单
        List<YqgConsumptionDetails> clocklist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);

        if(clocklist.size()==0){
            yqgConsumptionDetails.setNextClockStatus("");
            //设置未退房
            yqgConsumptionDetails.setCheckOutStatus("1");
            List<YqgConsumptionDetails> checklist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            if (checklist.size()==0){
                return  1;
            }
            else {
                //未退房
                return  2;
            }

        }
        else{
            //未下钟
            return  3;
        }

    }
}
